package com.zhentao.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.zhentao.dto.ApplyDto;
import com.zhentao.dto.PunishDto;
import com.zhentao.dto.RecordDto;
import com.zhentao.dto.RewardDto;
import com.zhentao.mapper.RewardTypeMapper; // 新增Mapper注入
import com.zhentao.mapper.PointsRuleMapper;
import com.zhentao.pojo.*;
import com.zhentao.service.*;
import com.zhentao.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import java.util.Date;

@RestController
@RequestMapping("point")
public class PointController {
    @Autowired
    private RewardTypeService rewardTypeService;
    @Autowired
    private PunishTypeService punishTypeService;
    @Autowired
    private PointsApplyService pointsApplyService;
    @Autowired
    private PointsRecordService pointsRecordService;
    @Autowired
    private RewardTypeMapper rewardTypeMapper; // 新增Mapper注入
    @Autowired
    private StudentService studentService;
    @Autowired
    private PointsRuleMapper pointsRuleMapper;
    //奖励表的增删改查
    @RequestMapping("addRewardType")
    public Result addRewardType(@RequestBody RewardType rewardType){
        rewardType.setStatus(1);
        rewardType.setCreateTime(new Date());
        rewardType.setRuleId(1);
        rewardTypeService.save(rewardType);
        return Result.OK();
    }

    @RequestMapping("delRewardType")
    public Result delRewardType(Integer id){
        rewardTypeService.removeById(id);
        return Result.OK();
    }
    @RequestMapping("findRewardTypeById")
    public Result findRewardTypeById(Integer id){
        RewardType byId = rewardTypeService.getById(id);
        return Result.OK(byId);
    }
    @RequestMapping("updateRewardType")
    public Result updateRewardType(@RequestBody RewardType rewardType){
        rewardTypeService.updateById(rewardType);
        return Result.OK();
    }
    @RequestMapping("findRewardType")
    public Result findRewardType(@RequestBody RewardDto rewardDto){
        Page<RewardType> rewardType = rewardTypeService.findRewardType(rewardDto);
        return Result.OK(rewardType);
    }
    //惩罚表的增删改查
    @RequestMapping("addPunishType")
    public Result addRewardType(@RequestBody PunishType punishType){
        punishType.setStatus(1);
        punishType.setCreateTime(new Date());
        punishType.setRuleId(2);
        punishTypeService.save(punishType);
        return Result.OK();
    }

    @RequestMapping("delPunishType")
    public Result delPunishType(Integer id){
        punishTypeService.removeById(id);
        return Result.OK();
    }
    @RequestMapping("findPunishTypeById")
    public Result findPunishTypeById(Integer id){
        PunishType byId = punishTypeService.getById(id);
        return Result.OK(byId);
    }
    @RequestMapping("updatePunishType")
    public Result updatePunishType(@RequestBody PunishType punishType){
        punishTypeService.updateById(punishType);
        return Result.OK();
    }
    @RequestMapping("findPunishType")
    public Result findPunishType(@RequestBody PunishDto punishDto){
        Page<PunishType> punishType = punishTypeService.findPunishType(punishDto);
        return Result.OK(punishType);
    }


    //提交加/减分审核
    @RequestMapping("submitApply")
    public Result submitApply(@RequestBody PointsApply apply) {
        // 1. 参数校验（根据实际字段名）
        if (apply.getStudentId() == null || apply.getApplyPoints() == null) {
            return Result.ERROR("参数不完整"); // Result.fail 或 Result.error，按你实际的Result类方法
        }
        // 2. 设置初始状态
        apply.setStatusId(0); // 假如0代表“待审核”，用实际的枚举或常量
        apply.setCreateTime(new Date());
        // 3. 保存
        pointsApplyService.save(apply);
        // 4. 返回
        return Result.OK();
    }
    //审核人查看所有审核信息
    @RequestMapping("listPendingApply")
    public Result listPendingApply(@RequestBody ApplyDto applyDto){
        Page<PointsApply> page = pointsApplyService.listPendingApply(applyDto);
        return Result.OK(page);
    }
    //讲师查看自己提交的所有审核
    @RequestMapping("myApplyList")
    public Result myApplyList(@RequestBody ApplyDto applyDto) {
        // 参数校验
        if (applyDto.getTeacherId() == null) {
            return Result.ERROR("teacherId不能为空");
        }
        int pageNum = applyDto.getCurrent() == null ? 1 : applyDto.getCurrent();
        int pageSize = applyDto.getSize() == null ? 10 : applyDto.getSize();

        // 调用Service
        Page<PointsApply> page = pointsApplyService.findMyApplyList(applyDto.getTeacherId(), pageNum, pageSize);

        return Result.OK(page);
    }

    //通过审核
    @RequestMapping("approveApply")
    public Result approveApply(Integer applyId, Integer reviewerId, String reviewComment, Double approvedPoints) {
        PointsApply apply = pointsApplyService.getById(applyId);
        if (apply == null || apply.getStatusId() == null || apply.getStatusId() != 1) {
            return Result.ERROR("申请不存在或状态不允许审核");
        }
        // 更新申请状态
        apply.setStatusId(1); // 1=已通过
        apply.setReviewerId(reviewerId);
        apply.setReviewTime(new Date());
        apply.setReviewComment(reviewComment);
        apply.setApprovedPoints(approvedPoints);
        pointsApplyService.updateById(apply);

        // 查询事由类型（加分/减分）
        String reasonType = "";
        PointsRule rule = pointsRuleMapper.selectById(apply.getRuleId());
        if (rule != null && rule.getRuleType() != null) {
            reasonType = rule.getRuleType();
        }

        // 写入积分变动记录
        PointsRecord record = new PointsRecord();
        record.setStudentId(apply.getStudentId());
        record.setApplyId(apply.getId());
        record.setChangePoints(approvedPoints); // 或 apply.getApplyPoints()
        record.setOperationType(apply.getRuleId() == 1 ? "加分" : "减分");
        record.setOperationBy(reviewerId);
        record.setOperationName("admin"); // 可查审核人姓名
        record.setReasonType(reasonType); // 事由类型
        record.setRemark(reviewComment);
        record.setCreateTime(new Date());
        pointsRecordService.save(record);

        return Result.OK();
    }
    //驳回审核
    @RequestMapping("rejectApply")
    public Result rejectApply(Integer applyId, Integer reviewerId, String reviewComment) {
        PointsApply apply = pointsApplyService.getById(applyId);
        if (apply == null || apply.getStatusId() == null || apply.getStatusId() != 1) {
            return Result.ERROR("申请不存在或状态不允许审核");
        }
        // 更新申请状态
        apply.setStatusId(2); // 3=已驳回
        apply.setReviewerId(reviewerId);
        apply.setReviewTime(new Date());
        apply.setReviewComment(reviewComment);
        apply.setApprovedPoints(null); // 驳回时批准分值应为null
        pointsApplyService.updateById(apply);
        // 不写入积分变动记录
        return Result.OK();
    }

    //讲师查看自己班级的所有积分记录
    @RequestMapping
    public Result myPointsRecord(@RequestBody RecordDto dto){
        Page<PointsRecord> record = pointsRecordService.findRecord(dto);
        return Result.OK(record);
    }


}
